package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.atguigu.jxc.dao.CustomerReturnListDao;
import com.atguigu.jxc.dao.CustomerReturnListGoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.CustomerReturnList;
import com.atguigu.jxc.entity.CustomerReturnListGoods;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨林
 * @create 2023-01-28 16:20 星期六
 * description:
 */
@Service
public class CustomerReturnListGoodsServiceImpl implements CustomerReturnListGoodsService {

    @Autowired
    CustomerReturnListGoodsDao customerReturnListGoodsDao;

    @Autowired
    CustomerReturnListDao customerReturnListDao;

    @Autowired
    GoodsService goodsService;

    /**
     * 获取退货商品的总数量
     * @param goodsId
     * @return
     */
    @Override
    public Integer getSumReturnGoods(Integer goodsId) {
        return customerReturnListGoodsDao.getSumReturnGoods(goodsId) == null ? 0 : customerReturnListGoodsDao.getSumReturnGoods(goodsId);
    }

    /**
     * 客户退货单保存
     * @param customerReturnList
     * @param customerReturnListGoodsStr
     * @return
     */
    @Override
    public ServiceVO save(CustomerReturnList customerReturnList, String customerReturnListGoodsStr) {

        //customerReturnListGoodsStr转换为实体类
        List<CustomerReturnListGoods> customerReturnListGoodsList =
                JSONArray.parseArray(customerReturnListGoodsStr, CustomerReturnListGoods.class);

        //保存customerReturnList
        customerReturnListDao.save(customerReturnList);

        //回显id
        Integer customerReturnListId = customerReturnList.getCustomerReturnListId();

        for (CustomerReturnListGoods customerReturnListGoods : customerReturnListGoodsList) {
            customerReturnListGoods.setCustomerReturnListId(customerReturnListId);

            Integer goodsNum = customerReturnListGoods.getGoodsNum();
            String goodsCode = customerReturnListGoods.getGoodsCode();
            //客户退货  添加库存
            boolean ifChange = goodsService.changeInventoryQuantity(goodsCode, goodsNum);
            if (ifChange == false){
                return new ServiceVO(ErrorCode.CHANGE_INVENTORY_QUANTITY_ERROR,ErrorCode.CHANGE_INVENTORY_QUANTITY_ERROR_MSG);
            }

        }

        //批量保存
        customerReturnListGoodsDao.bachSave(customerReturnListGoodsList);

        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    /**
     * 退货单商品信息
     * @param customerReturnListId
     * @return
     */
    @Override
    public Map<String, Object> goodsList(Integer customerReturnListId) {

        Map<String, Object> map = new HashMap<>();
        List<CustomerReturnListGoods> customerReturnListGoodsList
                = customerReturnListGoodsDao.goodsList(customerReturnListId);
        map.put("rows",customerReturnListGoodsList);
        return map;
    }
}
